package com.mall.db.api;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mall.db.mapper.UmsMenuMapper;
import com.mall.pojo.UmsMenu;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@DubboService
public class UmsMenuApiImpl implements UmsMenuApi {

    @Autowired
    private UmsMenuMapper menuMapper;

    /**
     * 查询所有菜单信息
     *
     * @return
     */
    @Override
    public List<UmsMenu> findAll() {
        LambdaQueryWrapper<UmsMenu> wrapper = new LambdaQueryWrapper<>();
        return menuMapper.selectList(wrapper);
    }

    /**
     * 根据一组菜单id查询菜单信息
     *
     * @param menuIds
     * @return
     */
    @Override
    public List<UmsMenu> findByIds(List<Long> menuIds) {
        return menuMapper.selectBatchIds(menuIds);
    }

    //添加后台菜单
    @Override
    public void create(UmsMenu umsMenu) {
        menuMapper.insert(umsMenu);
    }

    //根据ID删除后台菜单
    @Override
    public void delete(Long id) {
        menuMapper.deleteById(id);
    }

    //分页查询后台菜单
    @Override
    public Page<UmsMenu> list(Integer pageNum, Integer pageSize, Long parentId) {
        Page<UmsMenu> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<UmsMenu> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UmsMenu::getParentId, parentId);
        return menuMapper.selectPage(page, wrapper);
    }

    //修改后台菜单
    @Override
    public void update(Long id, UmsMenu umsMenu) {
        LambdaQueryWrapper<UmsMenu> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UmsMenu::getId, id);
        menuMapper.update(umsMenu, wrapper);
    }

    //修改菜单显示状态
    @Override
    public void updateHidden(Long id, Integer hidden) {
        LambdaQueryWrapper<UmsMenu> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UmsMenu::getId, id);
        UmsMenu umsMenu = new UmsMenu();
        umsMenu.setHidden(hidden);
        menuMapper.update(umsMenu, wrapper);
    }

    //根据ID获取菜单详情
    @Override
    public UmsMenu selectById(Long id) {
        return menuMapper.selectById(id);
    }

    @Override
    public List<UmsMenu> selectAll() {
        LambdaQueryWrapper<UmsMenu> wrapper = new LambdaQueryWrapper<>();
        return menuMapper.selectList(wrapper);
    }
}
